#include "phoneloginwidget.h"
#include "model/data.h"

PhoneLoginWidget::PhoneLoginWidget(QWidget *parent)
    : QWidget{parent}
{
    this->setFixedSize(400, 350);
    this->setWindowTitle("登录");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setAttribute(Qt::WA_DeleteOnClose);    // 如果是在栈上定义的变量, 不能 delete, 会使程序崩溃.
    this->setStyleSheet("QWidget { background-color: white; }"
                        "QLineEdit { padding-left: 5px; font-size: 14px; border-radius: 5px; background-color: rgb(235, 235, 235); }"
                        "QPushButton { border: none; border-radius: 5px;} "
                        "QPushButton:pressed { background-color: rgb(235, 235, 235); }");

    // 1.网格布局
    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(50, 0, 50, 0);
    layout->setSpacing(10);
    layout->setAlignment(Qt::AlignVCenter);
    this->setLayout(layout);

    // 2.0标题
    m_tipLabel = new QLabel();
    m_tipLabel->setText("登录");
    m_tipLabel->setAlignment(Qt::AlignCenter);
    m_tipLabel->setFixedHeight(40);
    m_tipLabel->setStyleSheet("QLabel {font-size: 30px;}");

    // 2.1电话
    m_phoneEdit = new QLineEdit();
    m_phoneEdit->setPlaceholderText("输入电话");
    m_phoneEdit->setFixedHeight(30);

    // 2.2验证码编辑栏
    m_verifyCodeEdit = new QLineEdit();
    m_verifyCodeEdit->setPlaceholderText("输入验证码");
    m_verifyCodeEdit->setFixedHeight(30);
    m_verifyCodeEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    // 2.3验证码
    m_verifyCodeBtn = new QPushButton("发送验证码");
    m_verifyCodeBtn->setFixedHeight(30);
    m_verifyCodeBtn->setStyleSheet("QPushButton {border: none;}");
    m_verifyCodeBtn->setCursor(Qt::PointingHandCursor);
    connect(m_verifyCodeBtn, &QPushButton::clicked, this, &PhoneLoginWidget::sendVerifyCode);
    connect(&m_timer, &QTimer::timeout, this, &PhoneLoginWidget::countDown);

    // 2.4登录
    m_submitBtn = new QPushButton("登录");
    m_submitBtn->setFixedHeight(30);
    m_submitBtn->setStyleSheet("QPushButton { color: white; background-color: rgb(44, 182, 61); border: none; border-radius: 5px;} "
                               "QPushButton:pressed { background-color: #06AD56; }");
    m_submitBtn->setCursor(Qt::PointingHandCursor);
    connect(m_submitBtn, &QPushButton::clicked, this, &PhoneLoginWidget::clickSubmitBtn);

    // 2.5. 创建切换⼿机号登录按钮 + 注册
    m_switchToUsernameBtn = new QPushButton("切换到用户名");
    m_switchToUsernameBtn->setFixedHeight(20);
    connect(m_switchToUsernameBtn, &QPushButton::clicked, this, [=]{
        LoginWidget* login = new LoginWidget();
        login->show();
        this->close();
    });
    m_switchModeBtn = new QPushButton("注册");
    m_switchModeBtn->setFixedHeight(20);
    connect(m_switchModeBtn, &QPushButton::clicked, this, &PhoneLoginWidget::switchMode);

    // 8. 添加到布局管理器中
    layout->addWidget(m_tipLabel, 0, 0, 1, 5);
    layout->addWidget(m_phoneEdit, 1, 0, 1, 5);
    layout->addWidget(m_verifyCodeEdit, 3, 0, 1, 3);
    layout->addWidget(m_verifyCodeBtn, 3, 3, 1, 2);
    layout->addWidget(m_submitBtn, 4, 0, 1, 5);
    layout->addWidget(m_switchToUsernameBtn, 5, 0, 1, 1);
    layout->addWidget(m_switchModeBtn, 5, 4, 1, 1);
}
///////////////////////////////////////
///             槽函数
///////////////////////////////////////
void PhoneLoginWidget::switchMode()
{
    if (m_isLoginMode) {
        // 切换到注册模式
        this->setWindowTitle("注册");
        m_tipLabel->setText("注册");
        m_submitBtn->setText("注册");
        m_switchModeBtn->setText("登录");
        m_isLoginMode = false;
    } else {
        // 切换到登录模式
        this->setWindowTitle("登录");
        m_tipLabel->setText("登录");
        m_submitBtn->setText("登录");
        m_switchModeBtn->setText("注册");
        m_isLoginMode = true;
    }
}

void PhoneLoginWidget::sendVerifyCode()
{
    QString phone = m_phoneEdit->text().trimmed();
    QString msg = model::verifyPhone(phone);
    if(phone.isEmpty()){
        Toast::showMessage("请输入电话");
        return ;
    }
    if(!msg.isEmpty()){
        Toast::showMessage(msg);
        return ;
    }
    m_phone = phone;
    m_timer.start(1000);

    auto dataCenter = model::DataCenter::getInstance();
    // connect(dataCenter, &model::DataCenter::getVerifyCodeDone,
    //         this, &PhoneLoginWidget::switchMode, Qt::UniqueConnection);
    dataCenter->getVerifyCodeAsync(phone);
}

void PhoneLoginWidget::clickSubmitBtn()
{
    QString phone = m_phoneEdit->text().trimmed();
    if(phone.isEmpty()){
        Toast::showMessage("请输入电话");
        return ;
    }
    QString msg = model::verifyPhone(phone);
    if(!msg.isEmpty()){
        Toast::showMessage(msg);
        return ;
    }

    QString verifyCode = m_verifyCodeEdit->text();
    if(verifyCode.isEmpty()){
        Toast::showMessage("请输入验证码");
        return ;
    }

    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    if(m_isLoginMode){
        connect(dataCenter, &model::DataCenter::phoneLoginDone,
                this, &PhoneLoginWidget::phoneLoginDone, Qt::UniqueConnection);
        dataCenter->phoneLoginAsync(m_phone, dataCenter->getVerifyCodeId(), verifyCode);
    }else{
        connect(dataCenter, &model::DataCenter::phoneRegisterDone,
                this, &PhoneLoginWidget::phoneRegisterDone, Qt::UniqueConnection);
        dataCenter->phoneRegisterAsync(m_phone, verifyCode);
    }
}

void PhoneLoginWidget::countDown()
{
    if(m_interval < 1){
        m_timer.stop();
        m_verifyCodeBtn->setText("发送验证码");
        m_verifyCodeBtn->setEnabled(true);
        m_interval = 30;
    }else{
        m_verifyCodeBtn->setEnabled(false);
        m_verifyCodeBtn->setText(QString::number(m_interval--) + " s");
    }
}

void PhoneLoginWidget::phoneLoginDone(bool success, const QString &err)
{
    if(success == false){
        Toast::showMessage(err);
        return;
    }

    auto mainWidget = MainWidget::getInstance();
    mainWidget->show();

    this->close();
}

void PhoneLoginWidget::phoneRegisterDone(bool success, const QString &err)
{
    if(success == false){
        Toast::showMessage(err);
        return;
    }

    m_verifyCodeEdit->clear();
    m_isLoginMode = false;
    m_interval = 0;
    switchMode();
}
